<?php
/**
 * Created by PhpStorm.
 * User: zhendong
 * Date: 2017/4/17
 * Time: 9:44
 */
namespace Backend\Controller;
use Think\Controller;

class AdOrderController extends BaseController
{

    protected $ad_model;
    protected $ad_order_model;
    protected $customer_model;
    protected $ad_delivery_model;

    public function __construct() {
        parent::__construct();
        $this->ad_model             = D('Ad');
        $this->ad_order_model       = D('AdOrder');
        $this->customer_model       = D('Customer');
        $this->ad_delivery_model    = D('AdDelivery');
    }

    public function index() {
        $num = !empty(I('get.n')) ? I('get.n') : C('USER_DEFINED_CONF.PAGE_LIST_ROWS');
        $key = !empty(I('get.k')) ? I('get.k') : '';

        $ad_order_lists = $this->ad_order_model->get_ad_order_all();
        $count = count($ad_order_lists);

        $page = new \Think\Page($count, $num);

        $limit = $page->firstRow.','.$page->listRows;

        $ad_order_lists = $this->ad_order_model->get_ad_order_all([], $limit);

        $page = $page->show();
        $this->assign('page', $page);
        $this->assign('lists', $ad_order_lists);
        $this->display();
    }

    public function add() {
        if(IS_POST) {
            $data = $_POST;
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time']   = strtotime($data['end_time']);
            if(!$this->ad_order_model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $this->ad_order_model->getError() ]);
            } else {
                $result = $this->ad_order_model->filter('htmlspecialchars')->add($data);
                if($result !== false) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '创建成功！']);
                } else {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '创建失败！']);
                }
            }

        } else {
            $customer_info = $this->customer_model->get_customer_info();
            $this->assign('customer', $customer_info);
            $this->display();

        }
    }

    public function edit() {
        if(IS_POST) {
            $data = $_POST;
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time']   = strtotime($data['end_time']);
            if(!$this->ad_order_model->create()) {
                $this->ajaxReturn(['status' => 'failed', 'msg' => $this->ad_order_model->getError()]);
            } else {
                if($this->ad_order_model->filter('htmlspecialchars')->save($data) !== false) {
                    $this->ajaxReturn(['status' => 'success', 'msg' => '修改成功！' ]);
                } else {
                    $this->ajaxReturn(['status' => 'failed', 'msg' => '修改失败！' ]);
                }
            }
        } else {
            $id = I('get.id');
            $where = ['o.id' => $id];
            $ad_order_list = $this->ad_order_model->get_ad_order_all($where);
            $customer_lists = $this->customer_model->get_customer_info();

            $this->assign('customer', $customer_lists);
            $this->assign('list', $ad_order_list['0']);
            $this->display();
        }
    }

    public function update() {

    }

    public function delete() {
        $id = I('post.id');

        $condition['ad_delivery']   = ['order_id' => $id];
        $condition['ad_order']      = ['id' => $id];

        $ad_delivery_info = $this->ad_delivery_model->get_ad_delivery_info($condition['ad_delivery'], 'id, order_id');

        $ad_order_info = $this->ad_order_model->get_ad_order_info($condition['ad_order'], 'id, cid, order_state');

        /*
        if(!empty($ad_order_info) && ($ad_order_info[0]['order_state'] == '1')) {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '当前订单状态不允许被删除！']);
        }
        */

        if (!empty($ad_delivery_info)) {
            $this->ajaxReturn(['status' => 'failed', 'msg' => '当前订单下含有关联广告投放！']);
        }

        if($this->ad_order_model->delete($id)) {
            $this->ajaxReturn(['status' => 'success', 'msg' => '操作成功！']);
        } else {
            $this->ajaxReturn(['status' => 'failed', 'msg' =>'操作失败！']);
        }
    }

    public function ad_list() {
        $data = $this->ad_model->get_ad();
        $this->assign([
            'data' => $data['data'],
            'page' => $data['page'],
        ]);

        $this->display('ad_list');
    }

}